

#import "NSString+Category.h"
#import <CommonCrypto/CommonDigest.h>


@implementation NSString (Category)


- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize{
//    NSDictionary *attrs = @{NSFontAttributeName : font};
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    if (font) {
        [attrs setObject:font forKey:NSFontAttributeName];
    }else{
        [attrs setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName];
    }
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

- (BOOL)isPureInt{
    NSScanner* scan = [NSScanner scannerWithString:self];
    NSInteger val;
    return[scan scanInteger:&val] && [scan isAtEnd];
}

- (BOOL)ap_isNullString{
    if (self && ![self isKindOfClass:[NSNull class]] && ![[self stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString:@""]) {
        return NO;
    }
    return YES;
}



- (NSString *)urlEncode{
//    NSString *charactersToEscape = @" \"#%/:<>?@[\\]^`{|}";
//    NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
    return [self stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}

- (NSString *)md5String{
    const char* original_str=[self UTF8String];
    unsigned char digist[CC_MD5_DIGEST_LENGTH]; //CC_MD5_DIGEST_LENGTH = 16
    CC_MD5(original_str, (uint)strlen(original_str), digist);
    NSMutableString* outPutStr = [NSMutableString stringWithCapacity:10];
    for(int  i =0; i<CC_MD5_DIGEST_LENGTH;i++){
        [outPutStr appendFormat:@"%02x", digist[i]];//小写x表示输出的是小写MD5，大写X表示输出的是大写MD5
    }
    return [outPutStr lowercaseString];

}

@end
